package com.haoxuewa.component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoxuewa.config.listener.OnLineCountListener;
import com.haoxuewa.dao.VisitQuantityDao;
import com.haoxuewa.entity.VisitQuantity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

@Component
public class Scheduler {
    private final VisitQuantityDao visitQuantityDao;

    @Autowired
    public Scheduler(VisitQuantityDao visitQuantityDao) {
        this.visitQuantityDao = visitQuantityDao;
    }


    @Scheduled(cron = "0 0 1 * * ?")
    public void visitCountOfDay() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        int dayTotal = 0;
        int[] totals = OnLineCountListener.realTimeOnLineCountList;
        for (int i : totals){
            dayTotal = dayTotal + i;
        }
        //update visitquantity set quantity = #{quantity} where dayOfWeek=#{dayOfWeek}
        //calendar.get(Calendar.DAY_OF_WEEK) - 1, dayTotal
        VisitQuantity visitQuantity=new VisitQuantity();
        visitQuantity.setQuantity(dayTotal);
        visitQuantityDao.update(visitQuantity,new QueryWrapper<VisitQuantity>().eq("dayOfWeek",calendar.getFirstDayOfWeek()-1));
        OnLineCountListener.realTimeOnLineCountList = null;
//        OnLineCountListener.realTimeOnLineCount = 0;
    }
}
